package cn.saltybe.analysis.crawler.wbtc.secondHandHouse;

import cn.saltybe.analysis.crawler.wbtc.common.WbtcHouseImage;
import cn.saltybe.analysis.entity.data.RentingHouse;
import cn.saltybe.analysis.entity.data.SecondHandHouse;
import cn.saltybe.analysis.service.SecondHandHouseServiceImpl;
import com.geccocrawler.gecco.annotation.PipelineName;
import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("wbtcSecondHandHousePipeline")
@PipelineName("wbtcSecondHandHousePipeline")
public class WbtcSecondHandHousePipeline implements Pipeline<WbtcSecondHandHouse> {
    private static SecondHandHouseServiceImpl secondHandHouseService;

    @Autowired
    public void setSecondHandHouseService(SecondHandHouseServiceImpl secondHandHouseService) {
        this.secondHandHouseService = secondHandHouseService;
    }

    @Override
    public void process(WbtcSecondHandHouse wbtcSecondHandHouse) {
        try {
            String typeId = "wbtc" + wbtcSecondHandHouse.getId();

            HttpRequest currentRequest = wbtcSecondHandHouse.getRequest();

            String province = currentRequest.getParameter("province");
            String city = currentRequest.getParameter("city");

            SecondHandHouse secondHandHouse = new SecondHandHouse();
            secondHandHouse.setCity(city);
            secondHandHouse.setDistrict(wbtcSecondHandHouse.getDistrict());
            secondHandHouse.setSource(currentRequest.getUrl());
            secondHandHouse.setTotal_price(Double.parseDouble(wbtcSecondHandHouse.getPrice()));
            secondHandHouse.setTitle(wbtcSecondHandHouse.getTitle());
            secondHandHouse.setAddress(wbtcSecondHandHouse.getAddress());
            for (String i : wbtcSecondHandHouse.getImage()) {
                if (i.length() > 0) {
                    secondHandHouse.setImage(i);
                    break;
                }
            }
            secondHandHouse.setType_id("wbtc" + wbtcSecondHandHouse.getId());
            secondHandHouse.setSquare(Double.parseDouble(wbtcSecondHandHouse.getSquare()));
            try {
                if (secondHandHouseService.has(typeId)) {
                    System.out.println("更新旧条目");
                    secondHandHouseService.update(secondHandHouse);
                    System.out.println("旧条目已更新");
                } else {
                    System.out.println("插入新条目");
                    secondHandHouseService.insert(secondHandHouse);
                    System.out.println("新条目已入库");
                }
                System.out.println("所在地\t" + province + " " + city + " " + secondHandHouse.getDistrict());
                System.out.println("类别ID\t" + typeId);
                System.out.println("URL\t" + currentRequest.getUrl());
                System.out.println("标题\t" + secondHandHouse.getTitle());
                System.out.println("地址\t" + secondHandHouse.getAddress());
                System.out.println("房屋面积售价\t" + secondHandHouse.getSquare() + "㎡" + "\t" + wbtcSecondHandHouse.getPrice() + "万元");
            } catch (Exception e) {
                System.out.println("条目置入数据库失败");
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
